﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Timers;
using Reborn_Server.App1AppHandler;
using Reborn_WorldServer;

namespace App1AppHandler
{
    public class Loot : ICloneable
    {
        public int ID = -1;
        public long GameUID = -1;
        public string Name = "";
        public string ClassName = "";
        public string ItemType = "";
        public string ImageURL = "";
        public bool CanConsume = false;
        public bool Bag = false;
        public bool Belt = false;
        public bool Equip = false;
        public string Description = "";
        public int PriceLow = 0;
        public int PriceHigh = 0;
        public decimal Weight = 0;
        public bool Lore = false;

        public string XmlData = "";

        //Triggers used by loot manager when this item is a ground spawn.
        public bool isGroundSpawn = false;
        public int respawnMinutes = 0;

        // -1 = Does not bind, -2 Binds on pickup, -3 Binds on kill  
        public int BoundTo = -1;

        //These are for the destroy timer
        private Timer timerDestroy = new Timer(1 * 60 * 1000);
        public AppHandler2 MainApp;
        public int MapID;
        public int Instance;
        public int MapX;
        public int MapY;

        //Constructor
        public Loot(AppHandler2 app)
        {
            MainApp = app;
            timerDestroy.Elapsed += new ElapsedEventHandler(timerDestroy_Elapsed);
        }

        
        private double _extendedDelayInterval = 3 * 60 * 60 * 1000; //3 Hours
        private double _timerDestroyInterval = 60 * 1000;
        public double timerDestroyInterval 
        {
            set {
                _timerDestroyInterval = value;
            }
            get{
                return _timerDestroyInterval;
            }
        }
        public void timerDestroyStart(int mapID, int instance, int mapX, int mapY, bool useExtendedDelay)
        {
            MapID = mapID;
            Instance = instance;
            MapX = mapX;
            MapY = mapY;

            timerDestroy.Stop();

            //We dont want janitor to remove character corpses, that will be handled by the Rez
            if (!(this is CorpseCharacter))
            {
                if (useExtendedDelay)
                    timerDestroy.Interval = _extendedDelayInterval;
                else
                    timerDestroy.Interval = _timerDestroyInterval;

                timerDestroy.Start();
            }
        }
        public void timerDestroyStop()
        {
            timerDestroy.Stop();
        }
        protected virtual void timerDestroy_Elapsed(object source, ElapsedEventArgs e)
        {
            MainApp.GetMap(MapID, Instance).LootMngr.TakeItem(MapX, MapY, GameUID);
        }

        protected void copyProperties(Loot copyTo)
        {
            copyTo.Bag = this.Bag;
            copyTo.Belt = this.Belt;
            copyTo.BoundTo = this.BoundTo;
            copyTo.CanConsume = this.CanConsume;
            copyTo.ClassName = this.ClassName;
            copyTo.Description = this.Description;
            copyTo.Equip = this.Equip;
            copyTo.GameUID = this.GameUID;
            copyTo.ID = this.ID;
            copyTo.ImageURL = this.ImageURL;
            copyTo.Instance = this.Instance;
            copyTo.ItemType = this.ItemType;
            copyTo.MapID = this.MapID;
            copyTo.MapX = this.MapX;
            copyTo.MapY = this.MapY;
            copyTo.Name = this.Name;
            copyTo.PriceHigh = this.PriceHigh;
            copyTo.PriceLow = this.PriceLow;
            copyTo.Weight = this.Weight;
            copyTo.timerDestroyInterval = this.timerDestroyInterval;
            copyTo.Lore = this.Lore;

            copyTo.XmlData = this.XmlData;
        }

        public virtual string toXML(Character sendingToCharacter)
        {
            string result = "";

            StringWriter stringWriter = new StringWriter();
            XmlTextWriter xmldoc = new XmlTextWriter(stringWriter);

            xmldoc.WriteStartElement("i");
            xmldoc.WriteAttributeString("a", ID.ToString());
            xmldoc.WriteAttributeString("b",GameUID.ToString());
            xmldoc.WriteAttributeString("c",Name.ToString());
            xmldoc.WriteAttributeString("d", ClassName.ToString());
            xmldoc.WriteAttributeString("e", ItemType.ToString());
            xmldoc.WriteAttributeString("f", ImageURL.ToString());
            xmldoc.WriteAttributeString("g", CanConsume.ToString());
            xmldoc.WriteAttributeString("h", Bag.ToString());
            xmldoc.WriteAttributeString("i", Belt.ToString());
            xmldoc.WriteAttributeString("j", Equip.ToString());
            xmldoc.WriteAttributeString("k", Description.ToString());
            xmldoc.WriteAttributeString("l", PriceLow.ToString());
            xmldoc.WriteAttributeString("m", PriceHigh.ToString());

            xmldoc.WriteAttributeString("n", Weight.ToString());
            xmldoc.WriteAttributeString("o", BoundTo.ToString());
            xmldoc.WriteAttributeString("p", Lore.ToString());

            xmldoc.WriteEndElement();
            xmldoc.Flush();

            xmldoc.Close();
            
            stringWriter.Flush();
            
            result = stringWriter.ToString();

            return result;
        }

        #region ICloneable Members

        public virtual object Clone()
        {
            Loot clonedLoot = new Loot(Managers.GameHandler);
            copyProperties(clonedLoot);

            return clonedLoot;
        }

        #endregion
    }
}

